#-*- coding: utf8 -*-
import BeautifulSoup, sys, re, urllib2, json

reload(sys) 
sys.setdefaultencoding('utf8') 
import sys,re,urllib2,cookielib
def download(url):
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)')]
    #print "try to open..."
    f = opener.open(url)
    #print "OK ..."
    s = f.read()
    f.close()
    #print s
    return s

def parser(content, out_file):
    emptyString = '"pins":[]'
    if -1 != content.find(emptyString):
        return 0
    beginString = '"pins":['
    endString = '}]};'
    beginOffset = content.find(beginString)
    if -1 == beginOffset:
        return -1
    content = content[beginOffset+len(beginString)-1:]
    #print content
    ret = content.find(beginString)
    #print 'beginoffset:',ret
    #assert -1 == ret
    endOffset = content.find(endString)
    #print endOffset
    if -1 == endOffset:
        return -1
    content = content[:endOffset+2]
    #print content
    pinList = json.loads(content)
    if len(pinList) == 0:
        return 0
    out_file.write(content+'\n')
    out_file.flush()
    last_pin_id = pinList[-1]['pin_id']
    return last_pin_id
    #for x in pinList:
        #print x
        #for k,v in x.items():
        #    print k, v
        #print '-'*20
        #print x['pin_id']
        #print x['file']['key']
        #break

#url = 'http://www.huaban.com/boards/1324624/?h59mb2dr&max=12046539&limit=20'
#url_format = 'http://www.huaban.com/boards/1324624/?h59mb2dr&max=%u&limit=20'
url_format = 'http://www.huaban.com/boards/%u/?h59mb2dr&max=%u&limit=20'
board_id = int(sys.argv[1])
pin_id   = int(sys.argv[2])
out_file = file(sys.argv[3], "w")
count = 0
while True:
    if 0 == pin_id:
        break;
    else:
        count += 1
        url = url_format % (board_id, pin_id)
        while True:
            content = download(url)
            print '[%02u] downloaded %s' % (count, url, )
            pin_id = parser(content, out_file)
            if pin_id == -1:
                print 'retry... %s' % (url,)
                continue
            elif pin_id > 0:
                break
            elif pin_id == 0:
                print 'hit the end.'
                sys.exit(0)
        print '[%02u] next pin_id: %u processed %s' % (count, pin_id, url, )
